﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using DataBaseConnection;

namespace CMSLibrary.TagsCreate.CustomFunction.Inside
{
    /// <summary>
    /// 标签配制的模块
    /// 1.  Tag(首页_新闻资讯_最新头条)
    /// 2.  Tag(首页_新闻资讯_最新头条,2,..)    指定标签名称,不限长度的参数
    /// </summary>
    public class Custom_SpecialTag : PInside
    {
        #region 参数成员        
        /// <summary>
        /// 标签索引关键词
        /// </summary>
        public string tagKey = string.Empty;
        #endregion

        #region 重写的方法
        protected override void SplitFormatParas(string sourceParas)
        {
            string[] paras = GetParasArray(sourceParas);
            //确定重载方法,由多到少 
            /********** 2 **********/
            try
            {
                tagKey = Convert.ToString(paras[0]);
                params_values = GetValuesForStartIndex(paras, 1);
                FunctionBurden = BurdenIndex.two;
                return;
            }
            catch { }
            /********** 1 **********/
            try
            {
                tagKey = Convert.ToString(paras[0]);
                FunctionBurden = BurdenIndex.one;
                return;
            }
            catch { throw new Exception(); }
        }

        public override string ToString()
        {
            string result = string.Empty;

            DataEntity.tab_TagConfig t = (DataEntity.tab_TagConfig)new Cache_Tag().Cache_getData(tagKey);
            if (!t.IsEmpty)
            {
                string templateText = t.tag_DataTemplate; //模板
                DataTable sourceDT = null;  //数据源
                if ((bool)t.tag_IsGetSource.Value)
                {
                    DataSet ds = GetSource(t.tag_SourceID.Value, params_values, 0, 0);
                    if (ds != null) { sourceDT = ds.Tables[0]; }
                }

                //替换模板数据
                TemplateRepeater tr = new TemplateRepeater(templateText, sourceDT, 0);
                tr.Parameters = params_values;
                tr.RepeatTimes = Convert.ToInt32(t.tag_RepeatTimes.Value);
                tr.ReplaceStart();
                result = tr.SourceText;

                if (t.tag_IsSignShow.Value)
                {
                    result = "\r\n<!-- 标签(" + t.tag_TagName + ") Start -->\r\n" + result +
                        "\r\n<!-- 标签(" + t.tag_TagName + ") End -->\r\n";
                }
            }
            
            return result;
        }
        
        #endregion

    }
}
